﻿
using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using System.Collections.Generic;
using OF.Session;
namespace OF.DAL
{
	/// <summary>
	/// 数据访问类:Orders
	/// </summary>
    public partial class Orders : Session.DALBase
	{
		public Orders()
		{}
		#region  BasicMethod
		/// <summary>
		/// 是否存在该记录
		/// </summary>
		public bool Exists(int id)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) from Orders");
			strSql.Append(" where id=@id");
			SqlParameter[] parameters = {
					new SqlParameter("@id", SqlDbType.Int,4)
			};
			parameters[0].Value = id;

			return DbHelperSQL.Exists(strSql.ToString(),parameters);
		}


		/// <summary>
		/// 增加一条数据
		/// </summary>
		public int Add(OF.Model.Orders model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("insert into Orders(");
			strSql.Append("ordersn,allprice,buyerid,phone,addtime,scheduledtime,population,remark,state,ordertype,sendaddress,sendprice,dishprice,sellerid,reason)");
			strSql.Append(" values (");
			strSql.Append("@ordersn,@allprice,@buyerid,@phone,@addtime,@scheduledtime,@population,@remark,@state,@ordertype,@sendaddress,@sendprice,@dishprice,@sellerid,@reason)");
			strSql.Append(";select @@IDENTITY");
			SqlParameter[] parameters = {
					new SqlParameter("@ordersn", SqlDbType.NVarChar,32),
					new SqlParameter("@allprice", SqlDbType.Decimal,9),
					new SqlParameter("@buyerid", SqlDbType.Int,4),
					new SqlParameter("@phone", SqlDbType.NVarChar,15),
					new SqlParameter("@addtime", SqlDbType.DateTime),
					new SqlParameter("@scheduledtime", SqlDbType.NVarChar,50),
					new SqlParameter("@population", SqlDbType.Int,4),
					new SqlParameter("@remark", SqlDbType.NVarChar,20),
					new SqlParameter("@state", SqlDbType.Int,4),
					new SqlParameter("@ordertype", SqlDbType.TinyInt,1),
					new SqlParameter("@sendaddress", SqlDbType.NVarChar,100),
					new SqlParameter("@sendprice", SqlDbType.Decimal,9),
					new SqlParameter("@dishprice", SqlDbType.Decimal,9),
					new SqlParameter("@sellerid", SqlDbType.Int,4),
					new SqlParameter("@reason", SqlDbType.NVarChar,30)};
			parameters[0].Value = model.ordersn;
			parameters[1].Value = model.allprice;
			parameters[2].Value = model.buyerid;
			parameters[3].Value = model.phone;
			parameters[4].Value = model.addtime;
			parameters[5].Value = model.scheduledtime;
			parameters[6].Value = model.population;
			parameters[7].Value = model.remark;
			parameters[8].Value = model.state;
			parameters[9].Value = model.ordertype;
			parameters[10].Value = model.sendaddress;
			parameters[11].Value = model.sendprice;
			parameters[12].Value = model.dishprice;
			parameters[13].Value = model.sellerid;
			parameters[14].Value = model.reason;

			object obj = DbHelperSQL.GetSingle(strSql.ToString(),parameters);
			if (obj == null)
			{
				return 0;
			}
			else
			{
				return Convert.ToInt32(obj);
			}
		}
		/// <summary>
		/// 更新一条数据
		/// </summary>
		public bool Update(OF.Model.Orders model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("update Orders set ");
			strSql.Append("ordersn=@ordersn,");
			strSql.Append("allprice=@allprice,");
			strSql.Append("buyerid=@buyerid,");
			strSql.Append("phone=@phone,");
			strSql.Append("addtime=@addtime,");
			strSql.Append("scheduledtime=@scheduledtime,");
			strSql.Append("population=@population,");
			strSql.Append("remark=@remark,");
			strSql.Append("state=@state,");
			strSql.Append("ordertype=@ordertype,");
			strSql.Append("sendaddress=@sendaddress,");
			strSql.Append("sendprice=@sendprice,");
			strSql.Append("dishprice=@dishprice,");
			strSql.Append("sellerid=@sellerid,");
			strSql.Append("reason=@reason");
			strSql.Append(" where id=@id");
			SqlParameter[] parameters = {
					new SqlParameter("@ordersn", SqlDbType.NVarChar,32),
					new SqlParameter("@allprice", SqlDbType.Decimal,9),
					new SqlParameter("@buyerid", SqlDbType.Int,4),
					new SqlParameter("@phone", SqlDbType.NVarChar,15),
					new SqlParameter("@addtime", SqlDbType.DateTime),
					new SqlParameter("@scheduledtime", SqlDbType.NVarChar,50),
					new SqlParameter("@population", SqlDbType.Int,4),
					new SqlParameter("@remark", SqlDbType.NVarChar,20),
					new SqlParameter("@state", SqlDbType.Int,4),
					new SqlParameter("@ordertype", SqlDbType.TinyInt,1),
					new SqlParameter("@sendaddress", SqlDbType.NVarChar,100),
					new SqlParameter("@sendprice", SqlDbType.Decimal,9),
					new SqlParameter("@dishprice", SqlDbType.Decimal,9),
					new SqlParameter("@sellerid", SqlDbType.Int,4),
					new SqlParameter("@reason", SqlDbType.NVarChar,30),
					new SqlParameter("@id", SqlDbType.Int,4)};
			parameters[0].Value = model.ordersn;
			parameters[1].Value = model.allprice;
			parameters[2].Value = model.buyerid;
			parameters[3].Value = model.phone;
			parameters[4].Value = model.addtime;
			parameters[5].Value = model.scheduledtime;
			parameters[6].Value = model.population;
			parameters[7].Value = model.remark;
			parameters[8].Value = model.state;
			parameters[9].Value = model.ordertype;
			parameters[10].Value = model.sendaddress;
			parameters[11].Value = model.sendprice;
			parameters[12].Value = model.dishprice;
			parameters[13].Value = model.sellerid;
			parameters[14].Value = model.reason;
			parameters[15].Value = model.id;

			int rows=DbHelperSQL.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}

		/// <summary>
		/// 删除一条数据
		/// </summary>
		public bool Delete(int id)
		{
			
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from Orders ");
			strSql.Append(" where id=@id");
			SqlParameter[] parameters = {
					new SqlParameter("@id", SqlDbType.Int,4)
			};
			parameters[0].Value = id;

			int rows=DbHelperSQL.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}
		/// <summary>
		/// 批量删除数据
		/// </summary>
		public bool DeleteList(string idlist )
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from Orders ");
			strSql.Append(" where id in ("+idlist + ")  ");
			int rows=DbHelperSQL.ExecuteSql(strSql.ToString());
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}


		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public OF.Model.Orders GetModel(int id)
		{
			
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select  top 1 id,ordersn,allprice,buyerid,phone,addtime,scheduledtime,population,remark,state,ordertype,sendaddress,sendprice,dishprice,sellerid,reason from Orders ");
			strSql.Append(" where id=@id");
			SqlParameter[] parameters = {
					new SqlParameter("@id", SqlDbType.Int,4)
			};
			parameters[0].Value = id;

			OF.Model.Orders model=new OF.Model.Orders();
			DataSet ds=DbHelperSQL.Query(strSql.ToString(),parameters);
			if(ds.Tables[0].Rows.Count>0)
			{
				return DataRowToModel(ds.Tables[0].Rows[0]);
			}
			else
			{
				return null;
			}
		}


		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public OF.Model.Orders DataRowToModel(DataRow row)
		{
			OF.Model.Orders model=new OF.Model.Orders();
			if (row != null)
			{
				if(row["id"]!=null && row["id"].ToString()!="")
				{
					model.id=int.Parse(row["id"].ToString());
				}
				if(row["ordersn"]!=null)
				{
					model.ordersn=row["ordersn"].ToString();
				}
				if(row["allprice"]!=null && row["allprice"].ToString()!="")
				{
					model.allprice=decimal.Parse(row["allprice"].ToString());
				}
				if(row["buyerid"]!=null && row["buyerid"].ToString()!="")
				{
					model.buyerid=int.Parse(row["buyerid"].ToString());
				}
				if(row["phone"]!=null)
				{
					model.phone=row["phone"].ToString();
				}
				if(row["addtime"]!=null && row["addtime"].ToString()!="")
				{
					model.addtime=DateTime.Parse(row["addtime"].ToString());
				}
				if(row["scheduledtime"]!=null)
				{
					model.scheduledtime=row["scheduledtime"].ToString();
				}
				if(row["population"]!=null && row["population"].ToString()!="")
				{
					model.population=int.Parse(row["population"].ToString());
				}
				if(row["remark"]!=null)
				{
					model.remark=row["remark"].ToString();
				}
				if(row["state"]!=null && row["state"].ToString()!="")
				{
					model.state=int.Parse(row["state"].ToString());
				}
				if(row["ordertype"]!=null && row["ordertype"].ToString()!="")
				{
					model.ordertype=int.Parse(row["ordertype"].ToString());
				}
				if(row["sendaddress"]!=null)
				{
					model.sendaddress=row["sendaddress"].ToString();
				}
				if(row["sendprice"]!=null && row["sendprice"].ToString()!="")
				{
					model.sendprice=decimal.Parse(row["sendprice"].ToString());
				}
				if(row["dishprice"]!=null && row["dishprice"].ToString()!="")
				{
					model.dishprice=decimal.Parse(row["dishprice"].ToString());
				}
				if(row["sellerid"]!=null && row["sellerid"].ToString()!="")
				{
					model.sellerid=int.Parse(row["sellerid"].ToString());
				}
				if(row["reason"]!=null)
				{
					model.reason=row["reason"].ToString();
				}
			}
			return model;
		}

		/// <summary>
		/// 获得数据列表
		/// </summary>
		public DataSet GetList(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select id,ordersn,allprice,buyerid,phone,addtime,scheduledtime,population,remark,state,ordertype,sendaddress,sendprice,dishprice,sellerid,reason ");
			strSql.Append(" FROM Orders ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			return DbHelperSQL.Query(strSql.ToString());
		}

		/// <summary>
		/// 获得前几行数据
		/// </summary>
		public DataSet GetList(int Top,string strWhere,string filedOrder)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select ");
			if(Top>0)
			{
				strSql.Append(" top "+Top.ToString());
			}
			strSql.Append(" id,ordersn,allprice,buyerid,phone,addtime,scheduledtime,population,remark,state,ordertype,sendaddress,sendprice,dishprice,sellerid,reason ");
			strSql.Append(" FROM Orders ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			strSql.Append(" order by " + filedOrder);
			return DbHelperSQL.Query(strSql.ToString());
		}

		/// <summary>
		/// 获取记录总数
		/// </summary>
		public int GetRecordCount(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) FROM Orders ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			object obj = DbHelperSQL.GetSingle(strSql.ToString());
			if (obj == null)
			{
				return 0;
			}
			else
			{
				return Convert.ToInt32(obj);
			}
		}
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("SELECT * FROM ( ");
			strSql.Append(" SELECT ROW_NUMBER() OVER (");
			if (!string.IsNullOrEmpty(orderby.Trim()))
			{
				strSql.Append("order by T." + orderby );
			}
			else
			{
				strSql.Append("order by T.id desc");
			}
			strSql.Append(")AS Row, T.*  from Orders T ");
			if (!string.IsNullOrEmpty(strWhere.Trim()))
			{
				strSql.Append(" WHERE " + strWhere);
			}
			strSql.Append(" ) TT");
			strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
			return DbHelperSQL.Query(strSql.ToString());
		}

		/*
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetList(int PageSize,int PageIndex,string strWhere)
		{
			SqlParameter[] parameters = {
					new SqlParameter("@tblName", SqlDbType.VarChar, 255),
					new SqlParameter("@fldName", SqlDbType.VarChar, 255),
					new SqlParameter("@PageSize", SqlDbType.Int),
					new SqlParameter("@PageIndex", SqlDbType.Int),
					new SqlParameter("@IsReCount", SqlDbType.Bit),
					new SqlParameter("@OrderType", SqlDbType.Bit),
					new SqlParameter("@strWhere", SqlDbType.VarChar,1000),
					};
			parameters[0].Value = "Orders";
			parameters[1].Value = "id";
			parameters[2].Value = PageSize;
			parameters[3].Value = PageIndex;
			parameters[4].Value = 0;
			parameters[5].Value = 0;
			parameters[6].Value = strWhere;	
			return DbHelperSQL.RunProcedure("UP_GetRecordByPage",parameters,"ds");
		}*/

		#endregion  BasicMethod

        #region  ExtensionMethod

        /// <summary>
        /// 修改订单状态
        /// </summary>
        public bool Update(string id,int state)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("update Orders set state="+state);
            strSql.Append(" where id="+id);
            int rows = DbHelperSQL.ExecuteSql(strSql.ToString());
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataTable GetInvalidOrderList()
        {
            string strSql = "select id,scheduledtime,state from Orders where state = 0 and scheduledtime <>'' and GETDATE()>LEFT(scheduledtime,15)";
            var ds = DbHelperSQL.Query(strSql);
            if (ds == null || ds.Tables[0] == null || ds.Tables[0].Rows.Count == 0)
                return null;
            else
                return ds.Tables[0];
        }

        //首次获取
        public DataSet GetOrderFirst(string wherestr)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append(" select * from (select top 2 id,ordersn,allprice,buyerid,phone,addtime,scheduledtime,population,remark,state, ordertype,sendaddress,sendprice,dishprice,sellerid  from Orders ");
            strSql.Append(" where " + wherestr + " order by id desc)a order by id asc");
            return DbHelperSQL.Query(strSql.ToString());
        }
        //更多数据
        public DataSet GetOrderUp(string id,string wherestr)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append(" select top 2 id,ordersn,allprice,buyerid,phone,addtime,scheduledtime,population,remark,state, ordertype,sendaddress,sendprice,dishprice,sellerid  from Orders ");
            strSql.Append(" where id<" + id + " and " + wherestr + " order by id desc");
            return DbHelperSQL.Query(strSql.ToString());
        }
        //最新数据
        public DataSet GetOrderDown(string id, string wherestr)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append(" select top 2 id,ordersn,allprice,buyerid,phone,addtime,scheduledtime,population,remark,state,ordertype,sendaddress,sendprice,dishprice,sellerid  from Orders ");
            strSql.Append(" where id>" + id + " and " + wherestr);
            return DbHelperSQL.Query(strSql.ToString());
        }
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetPageList(string strWhere, string orderby, int pageindex, int pageSize)
        {
            return GetListByPage("Orders", "id", strWhere, orderby, pageindex, pageSize);
        }


        /// <summary>
        /// 获得数据列表
        /// </summary>
        public List<OF.Model.Orders> GetModelList(string strWhere)
        {
            DataSet ds = GetList(strWhere); if (ds != null && ds.Tables[0].Rows.Count > 0) { if (ds != null && ds.Tables[0].Rows.Count > 0) { return DataTableToList(ds.Tables[0]); } else { return null; } } else { return null; }
        }
        /// <summary>
        /// 获得数据列表
        /// </summary>
        public List<OF.Model.Orders> DataTableToList(DataTable dt)
        {
            List<OF.Model.Orders> modelList = new List<OF.Model.Orders>();
            int rowsCount = dt.Rows.Count;
            if (rowsCount > 0)
            {
                OF.Model.Orders model;
                for (int n = 0; n < rowsCount; n++)
                {
                    model = DataRowToModel(dt.Rows[n]);
                    if (model != null)
                    {
                        modelList.Add(model);
                    }
                }
            }
            return modelList;
        }
        #endregion  ExtensionMethod
    }
}

